package com.aaa.home.service.Impl;

import com.aaa.common.entity.AclRolePermission;
import com.aaa.common.entity.AclUserRole;
import com.aaa.common.entity.home.TblZone;
import com.aaa.common.vo.Result;
import com.aaa.home.dao.TblZoneDao;
import com.aaa.home.service.TblZoneService;
import com.aaa.home.vo.ZoneAddVo;
import com.aaa.home.vo.ZoneVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author ：Mickey
 * @date ：Created in 2022/12/6 22:51
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TblZoneServiceImpl implements TblZoneService {

    @Autowired
    private TblZoneDao zoneDao;

    @Autowired
    private TblZoneService zoneService;
    //分页查询
    @Override
    public Result getAllZonePage(ZoneVo zoneVo, Integer current, Integer pageSize) {
        IPage<TblZone> page = new Page<>(current, pageSize);
        QueryWrapper<TblZone> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(zoneVo.getZoneName())){
            wrapper.like("zone_name",zoneVo.getZoneName());
        }
        if (zoneVo.getIsDisable()!=null){
            wrapper.eq("is_disable",zoneVo.getIsDisable());
        }
        IPage<TblZone> page1 = zoneDao.selectPage(page,wrapper);
        return new Result(2000,"分页查询专区成功",page1);
    }

    //改变状态
    @Override
    public Result updateZoneStatus(String id, Integer isDisable) {
        TblZone tblZone = new TblZone(id, isDisable);
        int i = zoneDao.updateById(tblZone);
        return new Result(2000,"修改专区状态成功",null);
    }
    //删除专区
    @Override
    public Result deleteZoneBtId(String id) {
        int i = zoneDao.deleteById(id);
        System.out.println(i+"删除专区是否成功");
        return new Result(2000,"删除专区成功",null);
    }

    //添加和修改(添加没有去重校验导致修改不成功)
    @Override
    public Result addZone(ZoneAddVo zoneAddVo) {
        System.out.println(zoneAddVo);
        String zoneDesc = zoneAddVo.getZoneDesc();
        String zoneName = zoneAddVo.getZoneName();
        QueryWrapper<TblZone> wrapper = new QueryWrapper<>();
        wrapper.eq("zone_name",zoneName);
        TblZone selectZone = zoneDao.selectOne(wrapper);
        System.out.println(selectZone+"aaaaaaaaaaaaaaaaaaaaaaaa");
        if(selectZone!=null){
            zoneService.deleteZoneBtId(selectZone.getId());
        }
        TblZone tblZone = new TblZone(LocalDateTime.now(),LocalDateTime.now(),"0",0,zoneName,zoneDesc);
        int insert = zoneDao.insert(tblZone);
        return new Result(2000,"添加专区成功",null);
    }
}
